package cn.wintersun.baseweb.controller.user;


import org.springframework.core.io.Resource;
import cn.wintersun.basecommon.common.Result;
import cn.wintersun.basecommon.context.BaseContext;
import cn.wintersun.baseweb.dao.FileDate;
import cn.wintersun.baseweb.service.intrer.user.FileDateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/filedata")
//@PreAuthorize("hasAuthority('1')")
@Slf4j
public class FIleDataController {

    @Autowired
    private FileDateService fileDateService;


    @GetMapping("/get")
    public Result<List<FileDate>> getFileDate(){
        Integer userId = Math.toIntExact(BaseContext.getCurrentId());
        List<FileDate> fileDateList = fileDateService.getFileDate(userId);
        return Result.ok(fileDateList);
    }

    @PostMapping("/delete")
    public Result<Object> deleteFileDate(@RequestParam  String filePath){
        fileDateService.deleteFileDate(filePath);
        return Result.ok();
    }
    @PostMapping("/update")
    public Result<Object> updateFileDate(@RequestBody FileDate fileDate){
        fileDateService.updateFileDate(fileDate);
        return Result.ok();
    }

    @PostMapping("/download")
    public ResponseEntity<Resource> downloadFile(@RequestParam("fileName") String fileName){
        return fileDateService.downloadFile(fileName);
    }
}
